/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.report;

import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionSupport;
import database.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import model.*;

/**
 *
 * @author Trang
 */
@Namespace("/Administration/Revenue")
public class WRevenueByDateAction extends ActionSupport{
    public IssuingDAO issueDAO = new IssuingDAO();
    private List<Issue> issues = new ArrayList();
    private String title;
    private String date;
    private double totalAmount;
   
    private String revenue;
    private Map<Integer,String> sorts;
    private Integer sort;

    public Map<Integer, String> getSorts() {
        return sorts;
    }

    public void setSorts(Map<Integer, String> sorts) {
        this.sorts = sorts;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }
    
    public WRevenueByDateAction()
    {
        sorts = new HashMap<Integer, String>();
        sorts.put(1, "Create Date");
        sorts.put(2, "Total Quantity");
        sorts.put(3, "Total Amount");
        sorts.put(4, "Customer");
    }

    public String getRevenue() {
        return revenue;
    }

    public void setRevenue(String revenue) {
        this.revenue = revenue;
    }

    

    public double getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(double totalAmount) {
        this.totalAmount = totalAmount;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
    
    

    public List<Issue> getIssues() {
        return issues;
    }

    public void setIssues(List<Issue> issues) {
        this.issues = issues;
    }
    @Action(value = "revenuebydate", results = {
        @Result(name = "success", location = "/manageReport/w_revenuebydate.jsp"),
        @Result(name = "input", location = "/manageReport/w_revenuebydate.jsp")
    })
    public String RevenueByDate(){
        //products = inventoryDAO.getProductForInventory();
//        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
//        Date createdate = new Date();
//        issues = issueDAO.getIssueByDate(createdate);
//        title = "Total Revenue in today";
//        date = dateFormat.format(createdate);
//        totalAmount = 0;
//        for(Issue item: issues)
//        {
//            totalAmount += item.getOrder().getTotalAmount();
//        }
        return SUCCESS;
    }
    
    @Action(value = "getrevenue", results = {
        @Result(name = "success", location = "/manageReport/w_displayRevenuebydate.jsp")
    })
    public String Revenue(){
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        //System.out.println("Revenue"+revenue);
        if("Current Year".equals(revenue))
        {
            //System.out.println("Trang");
            String year = Calendar.getInstance().get(Calendar.YEAR)+"";
            //System.out.println(year);
            issues = issueDAO.getIssueByDate(year);
            title = "Total Revenue in "+year;
            //date = year;
        }
        //2013-10-23
        if("Current Month".equals(revenue))
        {
            //System.out.println("Trang");
            int month1 = Calendar.getInstance().get(Calendar.MONTH)+1;
            String year =Calendar.getInstance().get(Calendar.YEAR)+"";
            String month = month1+"";     
            System.out.println(month);
            issues = issueDAO.getIssueByDate(year+"-"+month+"-");
            title = "Total Revenue in "+month+" - "+Calendar.getInstance().get(Calendar.YEAR)+"";
            //date = year;
        }
        if("Current Day".equals(revenue))
        {
            //System.out.println("Trang");
            Date createdate = new Date();
            issues = issueDAO.getIssueByDate(createdate);
            title = "Total Revenue in today";
            date = dateFormat.format(createdate);
            //date = year;
        }
        totalAmount = 0;
        for(Issue item: issues)
        {
            totalAmount += item.getOrder().getTotalAmount();
        }
        return SUCCESS;
    }
}
